package com.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import com.alibaba.fastjson.JSON;

/**
 * 通用上传
 * @author: Bdqn.Guo
 * @date: 2018年4月14日 上午10:42:56  
 * @Description: TODO
 */
@WebServlet("/upload")
public class UploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UploadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		Map<String, Object> resultMap = new HashMap<String, Object>(2);
		try {
			//创建工厂类对象
			FileItemFactory factory = new DiskFileItemFactory();
			//上传文件对象
			ServletFileUpload fileUpload = new ServletFileUpload(factory);
			//设置转换的编码
			fileUpload.setHeaderEncoding("UTF-8");
			//解析form表单数据
			List<FileItem> fileItems =  fileUpload.parseRequest(request);
			//获取每一个文件
			for(FileItem fileItem : fileItems){
				//判断是否是文件类型
				if(!fileItem.isFormField()){
					//要保存的文件名
					String fileName = new Date().getTime() + "_" + fileItem.getName();
					//保存到服务器目录
					String realPath = "E:/s.g.com/image/" + fileName;//application.getRealPath("upload/") + fileName;
					//复制文件 通过文件流方式
					IOUtils.copy(fileItem.getInputStream(), new FileOutputStream(new File(realPath)));
					resultMap.put("fileName", fileName);
				}
			}
			resultMap.put("flag", true);
			resultMap.put("message", "上传成功");
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("flag", false);
			resultMap.put("message", "上传失败");
			;
		}
		PrintWriter out =  response.getWriter();
		out.print(JSON.toJSONString(resultMap));
		out.flush();
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
